package com.yzt.zhuju.dialog;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.yzt.zhuju.R;
import com.yzt.zhuju.constants.IntentConstant;

/**
 * author : bob
 * date   : 2022/11/30
 * desc   :
 */
public class DefaultDialog extends CenterBaseDialog {

    private CountDownTimer countDownTimer;

    protected TextView mSubContextTv;

    private TextView mSubmitTv;

    private boolean isCancel = true;

    private OnSelectOnClickListener listener;

    private OnHighlightTextClickListener onHighlightTextClickListener;

    public void setOnHighlightTextClickListener(OnHighlightTextClickListener onHighlightTextClickListener) {
        this.onHighlightTextClickListener = onHighlightTextClickListener;
    }

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_fragment_default;
    }

    @Override
    public void bindView(View v) {

        if (getArguments() == null) {
            dismissAllowingStateLoss();
            return;
        }

        Bundle bundle = getArguments();
        String content = bundle.getString(IntentConstant.INTENT_CONTENT);
        String title = bundle.getString(IntentConstant.INTENT_TITLE);
        String cancel = bundle.getString(IntentConstant.INTENT_CANCEL);
        String submit = bundle.getString(IntentConstant.INTENT_SUBMIT);
        String subContent = bundle.getString(IntentConstant.INTENT_SUB_CONTENT);
        String highlightText = bundle.getString(IntentConstant.INTENT_HIGHLIGHT_TEXT);
        String rewardDesc = bundle.getString(IntentConstant.INTENT_REWARD_DESC);
        String guardDesc = bundle.getString(IntentConstant.INTENT_GUARD_DESC);
        String guardNumber = bundle.getString(IntentConstant.INTENT_GUARD_NUMBER);
        boolean contentBold = bundle.getBoolean(IntentConstant.INTENT_CONTENT_BOLD, true);
        int minutes = bundle.getInt(IntentConstant.INTENT_MINUTES);
        int highlightColor = bundle.getInt(IntentConstant.INTENT_HIGHLIGHT_COLOR);
        int expired = bundle.getInt(IntentConstant.INTENT_EXPIRED);
        boolean isShow = bundle.getBoolean(IntentConstant.INTENT_SHOW_CLOSE);

        isCancel = bundle.getBoolean(IntentConstant.INTENT_CANCEL_MODEL, true);

        TextView mContentTv = v.findViewById(R.id.content_tv);
        TextPaint paint = mContentTv.getPaint();
        paint.setFakeBoldText(contentBold);
        TextView mCancelTv = v.findViewById(R.id.cancel_tv);
        mSubmitTv = v.findViewById(R.id.submint_tv);
        mSubContextTv = v.findViewById(R.id.content_sub_tv);
        ImageView ivClose = v.findViewById(R.id.iv_close);
        TextView tvTitle = v.findViewById(R.id.tv_title);
        TextView contentTvSpannable = v.findViewById(R.id.content_tv_spannable);

        if (isShow) {
            ivClose.setVisibility(View.VISIBLE);
        } else {
            ivClose.setVisibility(View.GONE);
        }

        if (!TextUtils.isEmpty(subContent)) {
            mSubContextTv.setVisibility(View.VISIBLE);
            mSubContextTv.setText(subContent);
        }

        if (!TextUtils.isEmpty(content)) {
            mContentTv.setText(content);
            contentTvSpannable.setVisibility(View.GONE);
        } else if (!TextUtils.isEmpty(rewardDesc) && minutes != 0) {
            content = "再玩聊天室 " + minutes + "分钟 就可以获得" + rewardDesc + "，确认退出吗 " + highlightText;
            SpannableString spStr = new SpannableString(content);
            int start = content.indexOf(highlightText);
            int startMinutes = content.indexOf(minutes + "分钟");
            int startRewardDesc = content.indexOf(rewardDesc);
            spStr.setSpan(new ClickableSpan() {
                @Override
                public void updateDrawState(TextPaint ds) {
                    super.updateDrawState(ds);
                    ds.setColor(highlightColor);       //设置文件颜色
                    ds.setUnderlineText(false);      //设置下划线
                }

                @Override
                public void onClick(View widget) {
                    if (onHighlightTextClickListener != null) {
                        onHighlightTextClickListener.onHighlightTextClick();
                    }
                }
            }, start, start + highlightText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

            spStr.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), startMinutes, startMinutes + (minutes + "分钟").length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            spStr.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), startRewardDesc, startRewardDesc + rewardDesc.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            contentTvSpannable.setMovementMethod(LinkMovementMethod.getInstance());
            contentTvSpannable.setText(spStr);
            contentTvSpannable.setVisibility(View.VISIBLE);
        } else if (!TextUtils.isEmpty(guardDesc) && !TextUtils.isEmpty(guardNumber)) {
            SpannableString spStr = new SpannableString(guardDesc);
            ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#FFFF4461"));
            spStr.setSpan(foregroundColorSpan, guardDesc.indexOf(guardNumber), guardDesc.indexOf(guardNumber) + guardNumber.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            contentTvSpannable.setText(spStr);
            contentTvSpannable.setVisibility(View.VISIBLE);
        }
        if (!TextUtils.isEmpty(title)) {
            tvTitle.setVisibility(View.VISIBLE);
            tvTitle.setText(title);
        } else {
            tvTitle.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(cancel)) {
            mCancelTv.setText(cancel);
        }
        if (!TextUtils.isEmpty(submit)) {
            if (expired != 0) {
                countDownTimer = new CountDownTimer(expired * 1000L, 1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        mSubmitTv.setText(submit + " " + (millisUntilFinished / 1000) + "s");
                    }

                    @Override
                    public void onFinish() {
                        dismiss();
                    }
                };
                countDownTimer.start();
            } else {
                mSubmitTv.setText(submit);
            }
        }

        mCancelTv.setOnClickListener(this);
        mSubmitTv.setOnClickListener(this);
        ivClose.setOnClickListener(this);
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        Window window = dialog.getWindow();
        if (window != null) {
            // 设置宽度为屏宽, 靠近屏幕底部。
            // 一定要设置Background，如果不设置，window属性设置无效
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

            WindowManager.LayoutParams params = window.getAttributes();
            params.gravity = getGravity();
            // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
            params.width = (int) getResources().getDimension(R.dimen.dp_290);
            params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            window.setAttributes(params);
        }
        if (!isCancel) {
            getDialog().setCanceledOnTouchOutside(false);
            setCancelable(false);
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.getId() == R.id.cancel_tv) {
            if (listener != null) {
                listener.selectCancel();
            }
            dismiss();
        } else if (v.getId() == R.id.submint_tv) {
            if (listener != null) {
                listener.selectSubmit();
            }
            dismiss();
        } else if (v.getId() == R.id.iv_close) {
            dismiss();
        }
    }

    public void setListener(OnSelectOnClickListener listener) {
        this.listener = listener;
    }

    public interface OnSelectOnClickListener {

        void selectSubmit();

        void selectCancel();
    }

    public interface OnHighlightTextClickListener {

        void onHighlightTextClick();
    }

    @Override
    public void onDestroy() {
        if (countDownTimer != null) {
            countDownTimer.cancel();
            countDownTimer = null;
        }
        super.onDestroy();
    }
}
